//
//  UIView+GestureRecognizer.m
//  Beautiful Travel
//
//  Created by Apple on 15/11/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "UIView+GestureRecognizer.h"
#import <objc/runtime.h>
static const char TapBlockKey = '\0';
static const char PanBlockKey = '\0';
static const char RotateBlockKey = '\0';
static const char SwipeBlockKey = '\0';
static const char LongPressBlockKey = '\0';
@implementation UIView (GestureRecognizer)
- (void) addTapAction:(UIGestureRecognizerBlock) block
{
    //self.multipleTouchEnabled = YES;
    self.userInteractionEnabled = YES;
    objc_setAssociatedObject(self, &TapBlockKey, block, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lonnie_tap:)]];
}

- (void) lonnie_tap:(UIGestureRecognizer*) tap
{
    [self handleGesture:tap key:&TapBlockKey];
}

- (void) addPanAction:(UIGestureRecognizerBlock) block
{
    self.userInteractionEnabled = YES;
    objc_setAssociatedObject(self, &PanBlockKey, block, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(lonnie_pan:) ];
    [self addGestureRecognizer:pan];
}
- (void) lonnie_pan:(UIGestureRecognizer*) pan
{
    [self handleGesture:pan  key:&PanBlockKey];
}
- (void) addRotateAction:(UIGestureRecognizerBlock) block
{
    self.userInteractionEnabled = YES;
    objc_setAssociatedObject(self, &RotateBlockKey, block, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    UIRotationGestureRecognizer * rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(lonnie_rotate:) ];
    [self addGestureRecognizer:rotate];
}
- (void) lonnie_rotate:(UIGestureRecognizer*)  rotate
{
   [self handleGesture:rotate key:&RotateBlockKey];
}
- (void) addSwipeAction:(UIGestureRecognizerBlock) block directions:(NSArray *)directions
{
    self.userInteractionEnabled = YES;
    objc_setAssociatedObject(self, &SwipeBlockKey, block, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    [directions enumerateObjectsUsingBlock:^(NSNumber * direction, NSUInteger idx, BOOL * _Nonnull stop) {
        UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(lonnie_swipe:) ];
        swipe.direction = [direction integerValue];
        [self addGestureRecognizer:swipe];
    }];

}
- (void) lonnie_swipe:(UIGestureRecognizer*) swipe
{
    [self handleGesture:swipe key:&SwipeBlockKey];
}

- (void) addLongPressAction:(UIGestureRecognizerBlock)block
{
    self.userInteractionEnabled = YES;
    objc_setAssociatedObject(self, &PanBlockKey, block, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    UILongPressGestureRecognizer * lp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lonnie_longpress:) ];
    [self addGestureRecognizer:lp];
}

- (void) lonnie_longpress:(UIGestureRecognizer*) lp
{
    [self handleGesture:lp key:&LongPressBlockKey];
}


- (void) handleGesture:(UIGestureRecognizer*) g key:(const void*) blockkey
{
    UIGestureRecognizerBlock block = objc_getAssociatedObject(self,blockkey);
    if (block) {
        block(g);
    }
}

@end
